//
//  HZBlockView.m
//  HZTC_Stock
//
//  Created by MayerF on 17/3/13.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZBlockView.h"
#import "HZBlockCollectionReusableView.h"
#import "HZBlockCollectionViewCell.h"

#import "HZOwnedStockModel.h"

#define kCellPadding 1

static NSString *const blockCellID = @"blockCell";
static NSString *const blockHeaderID = @"blockHeader";

@interface HZBlockView()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong)UICollectionView *pCollectionView;
@property (nonatomic, strong)NSArray          *pListData;
@property (nonatomic, strong)NSArray          *pHeaderTitleArray;
@end

@implementation HZBlockView

#pragma mark -
#pragma mark 生命周期
- (instancetype)init{
    self = [super init];
    if (self) {
        [self p_commonInit];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)p_commonInit
{
    UICollectionViewFlowLayout *pFlowLayout = [UICollectionViewFlowLayout new];
    pFlowLayout.sectionHeadersPinToVisibleBounds = YES;
    _pCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:pFlowLayout];
    _pCollectionView.dataSource = self;
    _pCollectionView.delegate = self;
    _pCollectionView.bounces = NO;
    _pCollectionView.backgroundColor = [UIColor clearColor]; //
    [_pCollectionView registerClass:[HZBlockCollectionViewCell class] forCellWithReuseIdentifier:blockCellID];
    [_pCollectionView registerClass:[HZBlockCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:blockHeaderID];
    
    [self addSubview:_pCollectionView];
    
    [self p_initConstraints];
    
}

#pragma mark -
#pragma mark private
- (void)p_initConstraints
{
    [_pCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self);
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
}
#pragma mark -
#pragma mark public
- (void)updateNewData:(NSArray *)data andHeaderTitle:(NSArray *)titleArray
{
    if (data && titleArray) {
        self.pListData = data;
        self.pHeaderTitleArray = titleArray;
        [_pCollectionView reloadData];
    }
}

#pragma mark -
#pragma mark UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.pListData.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSArray *pTempRows = self.pListData[section];
    if (pTempRows.count) {
        return pTempRows.count;
    }else{
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *pTempArray = self.pListData[indexPath.section];
    if (pTempArray.count == 0) {return nil;}
    HZOwnedStockModel *model = pTempArray[indexPath.row];
    HZBlockCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:blockCellID forIndexPath:indexPath];
    
    NSString *name = model.model_Name;
    if (name.length > 4) {
        name = [name substringToIndex:4];
    }
    cell.pName.text = name;
    cell.pNum.text = model.model_ZhangFu;
    cell.pNum.textColor = model.model_Color;
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        HZBlockCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:blockHeaderID forIndexPath:indexPath];
        if(!headerView) {
            headerView = [[HZBlockCollectionReusableView alloc] init];
        }
        if (self.pHeaderTitleArray.count < (indexPath.section + 1)) {return nil;}
        NSString *title = self.pHeaderTitleArray[indexPath.section];
        [headerView setHeaderTitle:title];
        headerView.parent = self;
        headerView.tag = indexPath.section;

        NSArray *tempData = _pListData[indexPath.section];
        if (tempData.count != 0) {
            headerView.isOpen = YES;
        }else {
            headerView.isOpen = NO;
        }
        
        return headerView;
    }
    
    return nil;
}

#pragma mark -
#pragma mark UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat fCellW = (CGRectGetWidth(self.frame))/4;
    CGSize sCellSize = {fCellW, fCellW*2/3};
    
    return sCellSize;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return (CGSize){CGRectGetWidth(self.frame),30};
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
#pragma mark -
#pragma mark UICollectionViewDelegateFlowLayout
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *pTempArray = self.pListData[indexPath.section];
    if (pTempArray.count == 0) {return ;}
    HZOwnedStockModel *model = pTempArray[indexPath.row];
    [self.parent upToParentWithObjc:self withType:kHZ_BlockListDetailClick_Action withParamter:model];
}

- (void)upToParentWithObjc:(id)objc withType:(NSInteger)type withParamter:(id)paramter {
    switch (type) {
        case kHZ_ClickMarketSectionHeader_Action:
        case kHZ_stockListMore_Action:
        {            
            [self.parent upToParentWithObjc:objc withType:type withParamter:paramter];
        }
            break;
        default:
            break;
    }
}

@end
